/**
 * 生成随机len位数字
 */
export const randomLenNum = (len: any, date: any) => {
	let random = '';
	random = Math.ceil(Math.random() * 100000000000000)
		.toString()
		.substr(0, len || 4);
	if (date) random = random + Date.now();
	return random;
};
/**
 * 判断路由是否相等
 */
export const diff = (obj1: any, obj2: any) => {
	delete obj1.close;
	var o1 = obj1 instanceof Object;
	var o2 = obj2 instanceof Object;
	if (!o1 || !o2) {
		return obj1 === obj2;
	}

	if (Object.keys(obj1).length !== Object.keys(obj2).length) {
		return false;
	}

	for (var attr in obj1) {
		var t1 = obj1[attr] instanceof Object;
		var t2 = obj2[attr] instanceof Object;
		if (t1 && t2) {
			return diff(obj1[attr], obj2[attr]);
		} else if (obj1[attr] !== obj2[attr]) {
			return false;
		}
	}
	return true;
};
/**
 * 判断数组是否相等
 */
export const arrEquals = (arr1: any, arr2: any) => {
	if (!arr1 || !arr2) {
		return false;
	}

	if (arr1.length != arr2.length) {
		return false;
	}

	for (var i = 0; i < arr2.length; i++) {
		if (arr1.indexOf(arr2[i]) == -1) {
			return false;
		}
	}

	for (var i = 0; i < arr1.length; i++) {
		if (arr2.indexOf(arr1[i]) == -1) {
			return false;
		}
	}
	return true;
};
/**
 * 笛卡儿积
 */
export const descartes = (array: any) => {
	if (array.length < 2) return array[0] || [];
	return [].reduce.call(array, function (col, set) {
		var res = [];
		col.forEach(function (c) {
			set.forEach(function (s) {
				var t = [].concat(Array.isArray(c) ? c : [c]);
				t.push(s);
				res.push(t);
			});
		});
		return res;
	});
};
// 根据字典的value显示label
export const findByValue = (value: any, dic: any) => {
	let result = '';
	if (dic == '') return value;
	if (typeof value == 'string') {
		let index = 0;
		index = findArray(dic, value);
		if (index != -1) {
			result = dic[index].label;
		} else {
			result = value;
		}
	} else if (value instanceof Array) {
		result = [];
		let index = 0;
		value.forEach((ele) => {
			index = findArray(dic, ele);
			if (index != -1) {
				result.push(dic[index].label);
			} else {
				result.push(value);
			}
		});
		result = result.toString();
	}
	return result;
};

export const findArray = (array: any, value: string) => {
	return array.findIndex((item: any, index: number) => {
		return item.dictValue === value;
	});
};

// 获取assets静态资源
export const getAssetsFile = (url: string) => {
	return new URL(`../assets/${url}`, import.meta.url).href;
};
